package com.kins.so.chatassistant.service.impl;

import java.sql.Wrapper;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.kins.so.chatassistant.entity.ChatJudge;
import com.kins.so.chatassistant.entity.ChatQuestion;
import com.kins.so.chatassistant.mapper.ChatJudgeMapper;
import com.kins.so.chatassistant.service.ChatJudgeService;
import com.kins.so.chatassistant.service.ChatQuestionService;
import com.kins.so.chatassistant.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description:
 * @Author: 申科
 * @Date:2025/11/26 17:17
 */

public class ChatJudgeServiceImpl extends ServiceImpl<ChatJudgeMapper, ChatJudge> implements ChatJudgeService {
    @Autowired
    private ChatQuestionService chatQuestionService;
    @Autowired
    private CommonUtils commonUtils;

    @Override
    public boolean saveOrUpdate(ChatJudge entity) {
        if(StringUtil.isNotEmpty(entity.getId())){
            SysUser sysUser = commonUtils.getCurrLoginUser();
            entity.preUpdate(sysUser.getUserNum());
            entity.setCreateDate(new Date());
        }
        LambdaQueryWrapper<ChatQuestion> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ChatQuestion::getQuestionId,entity.getQuestionId());
        ChatQuestion chatQuestion = this.chatQuestionService.getOne(wrapper);
        if(chatQuestion!=null){
            entity.setQuestionId(chatQuestion.getId());
        }
        return this.saveOrUpdate(entity);
    }

}
